package linklist;

public class MyLinkList {
    public static class listNode {
        public int val;
        public listNode next;

        public listNode(int val) {
            this.val = val;
        }
    }

    public listNode hand;

    // 用链表求两数相加
    public listNode addTwoNumbers(listNode l1, listNode l2) {
        // 对新建指针进行初始化
        listNode pre = new listNode(0);
        listNode car = pre;
        // count 来表示进位
        int count = 0;
        while (l1 != null || l2 != null) {
            // 用0来代替长度短的链表
            int x = l1 == null ? 0 : l1.val;
            int y = l2 == null ? 0 : l2.val;
            int sum = x + y + count;
            count = sum / 10;
            sum = sum % 10;
            car.next = new listNode(sum);
            car = car.next;
            if (l1 != null) {
                l1 = l1.next;
            }
            if (l2 != null) {
                l2 = l2.next;
            }
        }
        if (count == 1) {
            car.next = new listNode(count);
        }
        return pre.next;
    }
}
